

package com.itheima.reggie.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.DishService;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
class DishServiceviceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public Page<Dish> findMy(Integer pageNum, Integer pageSize, String name) {
        //分页
        Page<Dish> page = new Page<>(pageNum,pageSize);
        // 准备封装查询条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //判断name是否为非空字符串
        wrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        page= dishMapper.selectPage(page,wrapper);
        //遍历菜表
        List<Dish> records = page.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            for (Dish record : records) {
                //根据菜品的分类id从分类表中查询分类信息
                Category category = categoryMapper.selectById(record.getCategoryId());


                record.setCategoryName(category.getName());
                //根据菜品id从口味表查询当前菜品口味列表

                LambdaQueryWrapper<DishFlavor> dish = new LambdaQueryWrapper<>();
                dish.eq(DishFlavor::getId,record.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dish);
                record.setFlavors(dishFlavors);
            }
        }
        return page;
    }

    @Override
    public List<Category> save(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType,type);
        return categoryMapper.selectList(wrapper);
    }

    @Override
    public void add(Dish dish) {
        //保存到表中
        dishMapper.insert(dish);
        //获取生成之后的id
        log.info("获取生成之后的id:"+dish.getId());
        //获取菜品口味列表
        List<DishFlavor> flavors = dish.getFlavors();
        //进行遍历
        if (CollectionUtil.isNotEmpty(flavors)){
            for (DishFlavor flavor : flavors) {
                //设置菜品id
                flavor.setDishId(dish.getId());
                //保存
                dishFlavorMapper.insert(flavor);
            }


        }
    }

    @Override
    public Dish findMyId(Long id) {
        // 从菜品表根据id查询
        Dish dish= dishMapper.selectById(id);
        //根据菜品的分类id从分类表中查询
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        // 根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
        dish.setFlavors(dishFlavorList);

        return dish;
    }

    @Override
    public void update(Dish dish) {
        //更新菜品信息
        dishMapper.updateById(dish);
        //删除口味表中当前菜品信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        //将修改之后口味列表重新保存到口味表中
        List<DishFlavor> flavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavors)) {
            for (DishFlavor flavor : flavors) {
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public void xs(Long[] ids,int id) {
        for (Long aLong : ids) {
            Dish dish = dishMapper.selectById(aLong);
            if (id!=0){
                dish.setStatus(1);
            }else if (id!=1){
                dish.setStatus(0);
            }
            dishMapper.updateById(dish);
        }
    }


    @Override
    public List<Long> delete(List<Long> ids) {
        dishMapper.deleteBatchIds(ids);
        return null;
    }

    @Override
    public List<Dish> findById(Long categoryId, Integer status) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId)
                .in(Dish::getStatus,status);
        List<Dish> list = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)){
            for (Dish dish : list) {
               // 得到每个菜品, 根据菜品的id从口味表找到当前菜品的口 味列表
                LambdaQueryWrapper<DishFlavor> ww = new LambdaQueryWrapper<>();
                ww.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(ww);
                dish.setFlavors(dishFlavors);
            }
        }
        return list;
    }
}
